<?php
if(!isset($authorizedPage)) require('error.php');

require('sampleimage.php');

define ('MAX_FILE_SIZE', 3145728);
define('UPLOAD_DIR', 'images/user/');

//http://cookbooks.adobe.com/post_Upload_image_to_website__PHP_-16389.html

//$_FILES['image']['name']
function imageUploadArray($imgFILE, $index = 0, $thumb = 1){

  // replace any spaces in original filename with underscores
  $file = str_replace(' ', '_', $imgFILE['name'][$index]);
  
  $ext = pathinfo($file, PATHINFO_EXTENSION);
  $file = rand_string(30) . "." . $ext;
  
  
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
'image/png');
  
  // upload if file is OK
  if (in_array($imgFILE['type'][$index], $permitted)
      && $imgFILE['size'][$index] > 0 
      && $imgFILE['size'][$index] <= MAX_FILE_SIZE) {
    switch($imgFILE['error'][$index]) {
      case 0:
        // check if a file of the same name has been uploaded
        if (!file_exists(UPLOAD_DIR . $file)) {
          // move the file to the upload folder and rename it
          $success =
move_uploaded_file($imgFILE['tmp_name'][$index], UPLOAD_DIR .
$file);
        } else {
          $result = 'ERROR 79'; //Should be impossible...
        }
        if ($success) {
          $result = $file; //Success
		  $image = new SimpleImage();
		  $image->load(UPLOAD_DIR . $file);
			
			if($thumb == 1){
				$image->resizeToHeight(300);
				$image->save(UPLOAD_DIR . 'largethumb/thumb' . $file, 100);
			}
			else{
				$image->resizeToHeight(225);
				$image->save(UPLOAD_DIR . 'normalthumb/thumb' . $file, 90);
			}
        } else {
          $result = "ERROR 70";
        }
        break;
      case 3:
      case 6:
      case 7:
      case 8:
        $result = "ERROR 70 ";
        break;
      case 4:
        $result = "None";
    }
  } else {
    $result = "Error 77 " . $imgFILE['name'][$index];
  }
  return $result;
}

function imageUpload($imgFILE, $thumb = 1){

  // replace any spaces in original filename with underscores
  $file = str_replace(' ', '_', $imgFILE['name']);
  
  $ext = pathinfo($file, PATHINFO_EXTENSION);
  $file = rand_string(30) . "." . $ext;
  
  
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
'image/png');
  
  // upload if file is OK
  if (in_array($imgFILE['type'], $permitted)
      && $imgFILE['size'] > 0 
      && $imgFILE['size'] <= MAX_FILE_SIZE) {
    switch($imgFILE['error']) {
      case 0:
        // check if a file of the same name has been uploaded
        if (!file_exists(UPLOAD_DIR . $file)) {
          // move the file to the upload folder and rename it
          $success =
move_uploaded_file($imgFILE['tmp_name'], UPLOAD_DIR .
$file);
        } else {
          $result = 'Error 79'; //Should be impossible...
        }
        if ($success) {
			$result = $file; //Success
			$image = new SimpleImage();
			$image->load(UPLOAD_DIR . $file);
			
			if($thumb == 1){
				$image->resizeToHeight(300);
				$image->save(UPLOAD_DIR . 'largethumb/thumb' . $file, 100);
			}
			else{
				$image->resizeToHeight(225);
				$image->save(UPLOAD_DIR . 'normalthumb/thumb' . $file, 90);
			}
        } else {
          $result = "Error 70";
        }
        break;
      case 3:
      case 6:
      case 7:
      case 8:
        $result = "Error 70 ";
        break;
      case 4:
        $result = "None";
    }
  } else {
    $result = "Error 77 " . $imgFILE['name'];
  }
  return $result;
}


$img_main = NULL;
$img_step = NULL;
$img_ingr = NULL;
$img_mate = NULL;


if (array_key_exists('createIt', $_POST)) {

	//echo "Image Cycling<br />";

	$img_main = "None";
	
	if(strlen($_FILES['image']['name']) > 0)
		$img_main = imageUpload($_FILES['image'], 1);

	
	foreach($_FILES['stepImage']['name'] as $key=>$val){
		
		$next = "None";
		
		if(strlen($_FILES['stepImage']['name'][$key]) > 0)
			$next = imageUploadArray($_FILES['stepImage'], $key, 2);
		$img_step .= $next. "%NEXT%";
	}
	
	foreach($_FILES['ingredientImage']['name'] as $key=>$val){
		
		$next = "None";
		
		if(strlen($_FILES['ingredientImage']['name'][$key]) > 0)
			$next = imageUploadArray($_FILES['ingredientImage'], $key, 2);
		$img_ingr .= $next. "%NEXT%";
	}
	
	foreach($_FILES['materialImage']['name'] as $key=>$val){
		
		$next = "None";
		
		if(strlen($_FILES['materialImage']['name'][$key]) > 0)
			$next = imageUploadArray($_FILES['materialImage'], $key, 2);
		$img_mate .= $next. "%NEXT%";
	}
  
}
?>